const Service = require("egg").Service;

class Blogservice extends Service {
    // 添加书籍（添加章节）
    async createBlog(body) {
        try {
            const blog = {
                title: body.title,
                img: body.img,
                orderby: body.orderby,
                md_text:body.md_text,
                html_text:body.html_text
            }
            await this.app.model.Blog.create(blog)
            return true;
        } catch (error) {
            return false;
        }
    }
    // //通过query查询条件查询博客列表
    // async getBlogList(query) {
    //     try {
    //         const number = parseInt(query.page)
    //         const start = number * 10 - 10
    //         const degree = parseInt(query.total)
    //         const blogList = await this.app.model.Blog.findAll({
    //             limit: [start, degree]
    //         })
    //         return blogList
    //     } catch (error) {
    //         return null
    //     }
    // }
    // 查询所有博客
    async getBlogList() {
        try {
            const blogList = await this.app.model.Blog.findAll()
            console.log("哈哈哈哈哈哈哈哈：",blogList);
            return blogList
        } catch (error) {
            return null
        }
    }
    // 按条件查询指定博客
   //查一篇博客
   async getBlogDetail(id) {
    try {
        const blog = await this.app.model.Blog.findOne({
            where: {
                id
            }
        })
        return blog
    } catch (error) {
        return null
    }
}
    // 修改指定博客
    async updateBlog(id, {title,md_text,html_text}) {
        try {
            await this.app.model.Blog.update({
                title,
                md_text,
                html_text
            }, {
                where: {
                    id
                }
            })
            return true
        } catch (error) {
            return false;;

        }
    }

    // 删除指定博客
    async deleteBlog(id) {
        try {
            this.app.model.Blog.destroy({
                where:{id}
            }
        )
            return true
        } catch (error) {
            return false;;

        }
    }
} 
module.exports = Blogservice;